package gw.com.android.ui.views;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.presenter.AdsData;

/**
 * 【首页】【我的】浮动广告
 *
 * @author jett
 * @since 2018-09-17.
 */
public class FloatAdverView extends LinearLayout {

    public static final int INDEX_HOME = 0;
    public static final int INDEX_MINE = 1;

    private static boolean homeClosed = false; // 首页 手动点击了关闭
    private static boolean mineClosed = false; // 我的 手动点击了关闭

    private ImageView ivClose;
    private ImageView ivAds;

    public FloatAdverView(Context context) {
        super(context);
    }

    public FloatAdverView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatAdverView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivClose = findViewById(R.id.iv_close);
        ivAds = findViewById(R.id.iv_float_ads);
    }

    public void refresh(final Activity activity, AdsData adsData, final int index) {
        if (isAdClosed(index)) {
            return;
        }
        if (ivAds == null) {
            return;
        }
        if (adsData == null || adsData.adPics == null || adsData.adPics.isEmpty()) {
            setVisibility(GONE);
            return;
        }
        AdsData.PicData picData = null;
        for (AdsData.PicData data : adsData.adPics) {
            if (data != null && String.valueOf(index).equals(data.sort)) {
                picData = data;
                break;
            }
        }
        if (picData == null) {
            setVisibility(GONE);
            return;
        }
        setVisibility(VISIBLE);
        // 控制悬浮广告
        Glide.with(activity)
                .load(picData.url)
                .placeholder(R.mipmap.a_default_adslip)
                .error(R.mipmap.a_default_adslip)
                .dontAnimate()//防止设置placeholder导致第一次不显示网络图片,只显示默认图片的问题
                .into(ivAds);
        //如果点击close，则关闭
        ivClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                markClosed(index);
                ivClose.setOnClickListener(null);
                ivAds.setOnClickListener(null);
                FloatAdverView.this.setVisibility(GONE);
            }
        });
        final String link = picData.link;
        final String title = picData.title;
        ivAds.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManager.showWebPageActivity(activity, title, link, false);
            }
        });
    }

    /**
     * 是否被手动关闭过
     */
    private boolean isAdClosed(int index) {
        if (index == INDEX_HOME) {
            return homeClosed;
        }
        if (index == INDEX_MINE) {
            return mineClosed;
        }
        return false;
    }

    private void markClosed(int index) {
        if (index == INDEX_HOME) {
            homeClosed = true;
        } else if (index == INDEX_MINE) {
            mineClosed = true;
        }
    }

}
